package com.android.demo.notepad2;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class NotesDbAdapter {
	
	public static final String KEY_ROWID = "_id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_DATE = "date";
	//public static final String KEY_TIME = "time";
	public static final String KEY_TIME_START = "start_time";
	public static final String KEY_TIME_END = "end_time";
	public static final String KEY_LOCATION = "location";
	public static final String KEY_CATEGORIES = "categories";
	public static final String KEY_DESCRIPTION = "description";
	
    private static final String TAG = "NotesDbAdapter";

//	public static String title;

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    
    private final Context mCtx;
    
    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, "schedule", null, 3);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL("create table events (_id integer primary key autoincrement, date text not null, "
                    + "title text not null, start_time text not null, end_time text not null, "
            		+ "location text not null, categories text not null, description text not null);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS events");
            onCreate(db);
        }
    }
    
    public boolean updateEvent(long rowId, String title, String date, String start_time, String end_time, String location, String categories, String description) {
        ContentValues args = new ContentValues();
        args.put(KEY_TITLE, title);
        args.put(KEY_DATE, date);
        args.put(KEY_TIME_START, start_time);
        args.put(KEY_TIME_END, end_time);
        args.put(KEY_LOCATION, location);
        args.put(KEY_CATEGORIES, categories);
        args.put(KEY_DESCRIPTION, description);

        return mDb.update("events", args, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public NotesDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    /**
     * Open the notes database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public NotesDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }


    /**
     * Create a new note using the title and body provided. If the note is
     * successfully created return the new rowId for that note, otherwise return
     * a -1 to indicate failure.
     * 
     * @param title the title of the note
     * @param body the body of the note
     * @return rowId or -1 if failed
     */
    public long createEvent(String title, String date, String start_time, String end_time, String location, String categories, String description) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_DATE, date);
        initialValues.put(KEY_TIME_START, start_time);
        initialValues.put(KEY_TIME_END, end_time);
        initialValues.put(KEY_LOCATION, location);
        initialValues.put(KEY_CATEGORIES, categories);
        initialValues.put(KEY_DESCRIPTION, description);

        return mDb.insert("events", null, initialValues);
    }

    /**
     * Delete the note with the given rowId
     * 
     * @param rowId id of note to delete
     * @return true if deleted, false otherwise
     */
    public boolean deleteEvent(long rowId) {

        return mDb.delete("events", KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
     * Return a Cursor over the list of all notes in the database
     * 
     * @return Cursor over all notes
     */
    public Cursor fetchAllEvents() {

        return mDb.query("events", new String[] {KEY_ROWID, KEY_TITLE, KEY_DATE, KEY_TIME_START, 
                KEY_TIME_END, KEY_LOCATION, KEY_CATEGORIES, KEY_DESCRIPTION}, null, null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at the note that matches the given rowId
     * 
     * @param rowId id of note to retrieve
     * @return Cursor positioned to matching note, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchEvent(long rowId) throws SQLException {

        Cursor mCursor =

            mDb.query(true, "events", new String[] {KEY_ROWID, KEY_TITLE, KEY_DATE,
            		KEY_TIME_START, KEY_TIME_END, KEY_LOCATION, KEY_CATEGORIES, KEY_DESCRIPTION}, KEY_ROWID + "=" + rowId,
            		null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }
}
